package com.mrjy.maintain.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrjy.common.entity.AccessLog;
import com.mrjy.common.entity.MenuInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.util.DateUtil;
import com.mrjy.common.util.ExcelUtil;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.maintain.dao.IAccessLogDao;
import com.mrjy.maintain.dao.IMenuInfoDao;

@Service
public class AccessLogBo implements IAccessLogBo{

	@Autowired
	private IAccessLogDao accessLogDao;
	
	@Autowired
	private IMenuInfoDao menuInfoDao;

	@Override
	public Object listByPage(AccessLog accessLog,HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		List<AccessLog> list = accessLogDao.listByPage(accessLog);
		Long total = accessLogDao.getTotal(accessLog);
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@Override
	public Object exportExl(AccessLog accessLog, HttpServletResponse response,HttpSession session){
		Map<String, Object> map = new HashMap<String, Object>();
		UserInfo loginUser = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if(null == loginUser ){
			map.put("err_code", -1);
			map.put("err_msg", "用户未登录");
			return map;
		}
		List<AccessLog> dataset = accessLogDao.listByPage(accessLog);
		if (dataset == null || dataset.size()==0) {
			map.put("err_code", 404);
			map.put("err_msg", "无可导出数据");
			return map;
		}
		for (AccessLog log : dataset) {
			if (log.getType().equals("1")) {
				log.setType("登录");
			}
			if (log.getType().equals("2")) {
				log.setType("退出");
			}
		}
		try {
			ExcelUtil.writeExcel(response, "访问日志"+DateUtil.dateToString(new Date(), DateUtil.FULL_DATE_STR)+".xls", dataset, AccessLog.class);
			map.put("err_code", 200);
			map.put("err_msg", "导出成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("err_code", 500);
			map.put("err_msg", "发生异常");
		}
		return map;
	}

	@Override
	public void txInsert(AccessLog accessLog, UserInfo userInfo) {
		//List<MenuInfo> list = menuInfoDao.queryChildMenuByRoleId(userInfo.getRoleId());
        StringBuffer sb = new StringBuffer();
        if (userInfo == null || userInfo.getRoleId() == null) {
            return;
        }

        List<MenuInfo> mList = menuInfoDao.queryMenuByRoleId(userInfo.getRoleId()); // 角色的所有菜单
		if (mList!=null&&mList.size()>0) { //角色有权限才遍历
			for (MenuInfo menuInfo : mList) {
				if (menuInfo.getMenuLevel() == 1) {
					sb.append(menuInfo.getMenuName() + ":");
					for (MenuInfo menuInfo2 : mList) {
						if (menuInfo2.getSuperMenu().equals(
								menuInfo.getMenuCode())) {
							sb.append(menuInfo2.getMenuName() + ",");
						}
					}
					sb.deleteCharAt(sb.length() - 1);
					sb.append("</br>");
				}
			}
		}
		accessLog.setAuthority(sb.toString());
        accessLog.setAccessTime(new Date());
        accessLog.setRoleName(userInfo.getRoleId()+"");
        accessLog.setUserName(userInfo.getId()+"");
		accessLogDao.insert(accessLog);
	}

	@Override
	public Object getTotal(AccessLog accessLog) {
		return accessLogDao.getTotal(accessLog);
	}

}
